/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

/**
 *
 * @author Nives
 */
public class ComentariBD {
    private QueryRunner run;
    private MapListHandler mlh;
    private MapHandler mh;
    
    public ComentariBD() throws NamingException, SQLException {
            Context ctx = new InitialContext();
            DataSource odsconn = (DataSource)ctx.lookup("jdbc/volansbd");
            run = new QueryRunner(odsconn);
            mlh = new MapListHandler();
            mh = new MapHandler();
    }

    public List<Map<String, Object>> consultaComentari(int id) throws SQLException {
        return run.query("select comentaris.comentari, comentaris.data, clients.nom, clients.cognoms from comentaris, clients where comentaris.id_client = clients.id_client AND comentaris.id_prod=? order by comentaris.data desc", mlh, id);
    }

    public boolean introdueixComentari( int id_prod, String comentari, int id_client ) throws SQLException {
        int num = run.update("INSERT INTO \"IG21254\".\"COMENTARIS\" (ID_COM, ID_PROD, COMENTARI, ID_CLIENT, data) VALUES (idcom.nextval, ?, ?, ?,current_date)",
                              id_prod, comentari, id_client);
        if (num == 0) {
            return false;
        }
        return true;
    }
}